<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\Card;
use app\models\CardReplace;
use app\models\KeyMap;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $replaceList app\models\CardReplace[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '卡替换列表';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl"></span>
            </label>
        </th>
        <th>旧卡</th>
        <th>新卡</th>
        <th>申请人</th>
        <th>申请时间</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($replaceList as $replace) {?>
        <tr id="data_<?php echo $replace->id;?>">
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $replace->id;?>" /><span class="lbl"><?php echo $replace->id;?></span></label></td>
            <td><?php echo Html::a($replace->fromCard->iccid, ['/user/card/view', 'id' => $replace->from_card_id]);?></td>
            <td><?php echo Html::a($replace->toCard->iccid, ['/user/card/view', 'id' => $replace->to_card_id]);?></td>
            <td><?php echo Html::a($replace->subUser->username, ['/user/user/view', 'id' => $replace->require_sub_uid]);?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($replace->create_time);?></td>
            <td><?php echo KeyMap::getValue('card_replace_status', $replace->status);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    !($replace->status == CardReplace::STATUS_WAIT) ?: ['icon' => 'fa fa-check', 'onclick' => 'acceptReplace(' . $replace->id . ')', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '通过', 'color' => 'green'],
                    !($replace->status == CardReplace::STATUS_WAIT) ?: ['icon' => 'fa fa-times', 'onclick' => 'rejectReplace(' . $replace->id . ')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '拒绝', 'color' => 'yellow'],
                    !($replace->fromCard->status != Card::STATUS_DEACTIVATED) ?: ['icon' => 'fa fa-power-off', 'onclick' => 'stopFromCard(' . $replace->from_card_id . ', ' . Card::STATUS_DEACTIVATED . ')', 'btn_class' => 'btn btn-xs btn-danger', 'tip' => '设置旧卡停机', 'color' => 'red'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script>
    /**
     * 通过
     * @param id 卡替换编号
     */
    function acceptReplace(id) {
        $.getJSON('<?php echo Url::to(['/user/card/accept-replace']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('已确认通过。');
                $('#data_' + id).remove();
            }
        });
    }

    /**
     * 拒绝
     * @param id 卡替换编号
     */
    function rejectReplace(id) {
        $.getJSON('<?php echo Url::to(['/user/card/reject-replace']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('已确认拒绝。');
                $('#data_' + id).remove();
            }
        });
    }

    /**
     * 设置旧卡停机
     * @param id 旧卡编号
     * @param status 停机状态
     */
    function stopFromCard(id, status) {
        if (!confirm('确定要设置旧卡停机吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/user/card/set-card-status']);?>', {id: id, status: status}, function (json) {
            if (callback(json)) {
                layer.msg('设置成功。');
            }
        });
    }
</script>
